package com.neo.base.fileTest

import scala.io.Source


object ReadFileTest {

  //v1
  def main(args: Array[String]): Unit = {
    if(args.length > 0){
      for (line <- Source.fromFile(args(0)).getLines)
        println(line.length + " " + line)
    }

    main1(args)
    main2(args)
  }


  def main1(args: Array[String]): Unit = {
    val lines = Source.fromFile(args(0)).getLines().toList

    println(lines)



    var maxWidth = 0
    for(line <- lines){
      maxWidth = maxWidth.max(widthOfLength(line))
    }



    for(line <- lines){
      val numSpaces  = maxWidth - widthOfLength(line)
      val padding =  " " * numSpaces
      print(padding  + line.length  + " | " + line)

    }

  }

  def widthOfLength(s: String) =  s.length.toString.length

  def main2(args: Array[String]) {
    def countchars4(args: Array[String]) {
      import scala.io.Source

      def widthOfLength(s: String) = s.length.toString.length

      if (args.length > 0) {

        val lines = Source.fromFile(args(0)).getLines.toList

        val longestLine = lines.reduceLeft(
          (a, b) => if (a.length > b.length) a else b
        )
        val maxWidth = widthOfLength(longestLine)

        for (line <- lines) {
          val numSpaces = maxWidth - widthOfLength(line)
          val padding = " " * numSpaces
          print(padding + line.length +" | "+ line)
        }
      }
      else
        Console.err.println("Please enter filename")
    }
    countchars4(Array("countchars1.scala"))
  }

}
